package LeetCode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/4/25.
// 
public class 有效的括号 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println(isValid(scan.next()));
    }
    public static  boolean isValid(String s) {
        if(s.length() == 0 || s.length() % 2!=0)return false;
        Stack<Character> temp =new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{'){
                temp.push(s.charAt(i));
            }else{
                if(temp.isEmpty()){
                    return false;
                }
                switch (temp.peek()){
                    case '(':
                        if (s.charAt(i) != ')'){
                            return false;
                        }
                        temp.pop();
                        break;
                    case '[' :
                        if(s.charAt(i) != ']'){
                            return false;
                        }
                        temp.pop();
                        break;
                    case '{' :
                        if(s.charAt(i) != '}'){
                            return false;
                        }
                        temp.pop();
                        break;
                }
            }
        }
        return temp.size() == 0;
    }
}

